import Vue from 'vue';
import store from "@/store"
export function throttle(fn, delay) {
	// 节流
	if (delay == null || delay == undefined) {
		delay = 1500
	}
	let _lastTime = null
	// 返回新的函数
	return function () {
		let _nowTime = + new Date()
		if (_nowTime - _lastTime > delay || !_lastTime) {
			fn.apply(this, arguments)   //将this和参数传给原函数
			_lastTime = _nowTime
		}
	}
}
export function debounce(fn, delay) {
	// 防抖
	let valid = true
	return function () {
		if (!valid) {
			//休息时间 暂不接客
			return false
		}
		// 工作时间，执行函数并且在间隔期内把状态位设为无效
		valid = false
		setTimeout(() => {
			fn()
			valid = true;
		}, delay)
	}
}
// 时间戳转时间格式 yyyy-MM-dd hh:mm:ss
export function formatDate(data, fmt) {
	if (typeof data == 'string' && data.indexOf('-') != -1) {
		//兼容ios
		data = data.replace(/-/g, "/")
	}
	let date = new Date(data)
	fmt = fmt ? fmt : 'yyyy-MM-dd hh:mm:ss'
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	};
	// 遍历这个对象
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
		}
	}
	return fmt;
}
export function hideTel(str) {
	// 脱敏
	if (typeof str == 'object') {
		return str
	}
	if (typeof str == 'number') {
		str = JSON.stringify(str)
	}
	if (str.length >= 11) {
		return str.substring(0, 3) + '****' + str.substring(str.length - 4)
	} else {
		return str
	}
}

export function padLeftZero(str) {
	return ('00' + str).substr(str.length);
}
export function checkPlatform() {
	//判断 ios android other
	let equipment = uni.getSystemInfoSync().platform
	return equipment
}
export function getPlatform() {
	/* 使用注释获取详细平台 */
	let platForm = undefined;
	// #ifdef APP-PLUS
	platForm = "APP";
	// #endif
	// #ifdef APP-PLUS-NVUE
	platForm = "APP";
	//#endif
	//#ifdef H5
	platForm = "H5";
	// #endif
	// #ifdef MP-WEIXIN
	platForm = "MP-WEIXIN";
	// #endif
	// #ifdef MP-ALIPAY
	platForm = "MP-ALIPAY";
	// #endif
	// #ifdef MP-BAIDU
	platForm = "MP-BAIDU";
	// #endif
	// #ifdef MP-TOUTIAO
	platForm = "MP-TOUTIAO";
	// #endif
	// #ifdef MP-QQ
	platForm = "MP-QQ";
	// #endif
	// #ifdef MP-360
	platForm = "MP-360";
	// #endif
	return platForm;
};
export function login() {
	// 登录
	let platForm = getPlatform()
	if (platForm == "MP-WEIXIN") {
		uni.navigateTo({
			url: "/pages/sys/login/wxLogin",
		})
	} else {
		uni.navigateTo({
			url: "/pages/sys/login/setIdentity",
		})
	}
}
export function checkTel(e) {
	// 检测手机号
	let reg = /^1[3456789]\d{9}$/
	return reg.test(e)
}
export function checkEmail(e) {
	// 检测邮箱
	let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
	return reg.test(e)
}
export function checkLogin(e, callback) {
	// 检测是否登录 没登录跳转登录
	// e用于检测后直接登录
	if (getStorageSync('lifeData')['vuex_token']) {
		return true
	} else {
		if (e) {
			typeof callback === 'function' && callback();
			uni.showModal({
				content: '请登录',
				success: (res) => {
					if (res.confirm) {
						login()
					} else {
					}
				}
			})
		}
		return false
	}
}
export function logOut() {
	if (window) {
		localStorage.clear();
	} else {
		uni.clearStorageSync()
	}
}
export function setStorageSync(key, val) {
	if (typeof val != "string" && val != null) {
		val = JSON.stringify(val)
	}
	let d = val
	uni.setStorageSync(key, d)
}
export function getStorageSync(key) {
	let a = uni.getStorageSync(key)
	let d = a
	try {
		return JSON.parse(d)
	} catch {
		return d
	}
}

